package tt.dz.appserver.mvc.manage;


import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.entity.model.push.PushMessage;
import tt.dz.appserver.service.business.PushMessageService;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.enums.Response;

/**
 * 消息管理类
 * @author xielf
 *
 */
@RestController
@RequestMapping("/manage/message")
public class MessageManager {

	/**消息管理服务类*/
	@Resource
	private PushMessageService pushMessageService;
	
	/**
	 * 资讯列表
	 * @param pageNo
	 */
	@RequestMapping(value="/pushMessageList",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String pushMessageList(@RequestParam(defaultValue="1") Integer pageNo){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			PushMessage pushMessage=new PushMessage();
			pushMessage.setState(1);
			pushMessage.setMessage_type(1);
			pushMessage.setScope(1);
			pushMessage.setAccount_sex(1);
			pushMessage.setValid_flag(1);
			pushMessage.setIs_push(0);
			pushMessage.setOn_line_user(0);
			resultMappingEntity = pushMessageService.messageList(pushMessage,pageNo);	
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 根据消息id获取消息详情
	 * @param pushId
	 * @return
	 */
	@RequestMapping(value="/messageDatail",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String pushMessageList(String pushId){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = pushMessageService.messageDetail(pushId);	
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 根据消息id删除消息
	 * @param pushId
	 * @return
	 */
	@RequestMapping(value="/deleteMessage",produces={"application/json;charset=utf-8"},method=RequestMethod.DELETE)
	public String deleteMessage(String pushId){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = pushMessageService.deleteMessage(pushId);	
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 保存消息传人参数
	 * @param title 消息标题
	 * @param content 消息内容
	 * @param editDate 消息编辑时间
	 * @param pushDate 消息推送时间
	 * @param state  消息状态1及时推送 2草稿 3延时推送
	 * @param delayDate 延时推送时间
	 * @param messageType 推送消息类型 1.普通消息推送；2系统更新推送；3活动推送等
	 * @param scope    推送消息范围 1.所有用户2.所有在线用户；3.注册3个月用户；4.其他自定义条件用户（定义后面3个用户字段限定）；
	 * @param regStartDate  注册时间 默认第一个用户注册时间
	 * @param regEndDate     截至注册时间 默认最后一个注册时间
	 * @param accountSex    用户性别  1.全部2男 3女
	 * @param pushUserId  推送操作员  
	 * @param pushOperator   推送用户范围 1.电桩用户2.所以运营商3华体用户
	 * @param sysCode   送系统端 1.所有用户端；2.app端3微信端
	 * @param isPush   是否已推送  1.已推送
	 * @param onLineUser  用户在线 0所以 1在线，2离线
	 * @return
	 */
	@RequestMapping(value="/savePushMessage",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String savePushMessage(@RequestParam(value="pushId",defaultValue="")String pushId,
			@RequestParam(value="title") String title,
			String content,String editDate,String pushDate,
			@RequestParam(value="state",defaultValue="1")Integer state,
			String delayDate,@RequestParam(value="messageType",defaultValue="1")int messageType,
			@RequestParam(value="scope",defaultValue="1")Integer scope,
			String regStartDate,String regEndDate,
			@RequestParam(value="accountSex",defaultValue="1")Integer accountSex,
			@RequestParam(value="pushUserId")String pushUserId,
			@RequestParam(value="pushOperator",defaultValue="1")String pushOperator,
			@RequestParam(value="sysCode")String sysCode,
			@RequestParam(value="isPush",defaultValue="0")Integer isPush,
			@RequestParam(value="onLineUser",defaultValue="0")Integer onLineUser){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = pushMessageService.saveMessage( pushId, title, content, editDate, pushDate,
					 state, delayDate, messageType, scope, regStartDate, regEndDate,
					 accountSex, pushUserId, pushOperator, sysCode, isPush,
					 onLineUser);	
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
}
